package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分页查询
     * @return
     */
    @Override
    public PageResult page(CategoryPageQueryDTO dto) {

        //设置分页参数
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //按条件查找
        Page<Category> categories = categoryMapper.pagination(dto);
        //创建分页对象
        PageResult pageResult = new PageResult( categories.getTotal(),categories.getResult());
        return pageResult;
    }

    /**
     * 新增分类
     * @param dto
     */
    @Override
    public void addCategory(CategoryDTO dto) {
        //创建分类对象
        Category category = new Category();
        //克隆dto对象
        BeanUtils.copyProperties(dto,category);
        //因为刚创建的分类下面没有菜品，所以设置为0停售
        category.setStatus(StatusConstant.DISABLE);

        //已使用aop自动填充
        //设置创建时间
        //category.setCreateTime(LocalDateTime.now());
        //category.setUpdateTime(LocalDateTime.now());
        //设置创建人
        //category.setCreateUser(BaseContext.getCurrentId());
        //category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.insertCategory(category);
    }

    /**
     * 启用禁用分类
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(int status, Long id) {
        Category category = Category.builder()
                .status(status)
                .id(id).build();
        categoryMapper.update(category);
    }

    /**
     * 修改分类
     * @param dto
     */
    @Override
    public void revice(CategoryDTO dto) {
        Category category = new Category();
        //拷贝对象
        BeanUtils.copyProperties(dto,category);

        //已使用aop自动填充
        //设置修改时间，和修改人id
        //category.setUpdateTime(LocalDateTime.now());
        //category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.update(category);
    }

    /**
     * 删除分类
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //删除分类前首先要判断，分类下面是否有菜品，或者套餐

        //查询分类下面是否包含了菜品，如果有菜品就抛出业务异常
        int count = dishMapper.countCategoryByid(id);
        if (count>0){
            throw new  DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //查询分类下是否包含套餐，如果有就抛出业务异常
        count = setmealMapper.countCategoryByid(id);
        if (count>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //该分类下什么都没有直接删除
        categoryMapper.deleteById(id);
    }

    /**
     * 根据类型查找分类
     * @param type
     * @return
     */
    @Override
    public List<Category> getCategoryById(int type) {
        List<Category> categories = categoryMapper.getCategoryById(type);

        return categories;
    }

    /**
     * 查询所有分类
     * @param type
     * @return
     */
    @Override
    public List<Category> getAllCategory(Integer type) {
        List<Category> categorys = categoryMapper.getAllCategory(type);
        return categorys;
    }
}
